package org.example.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadUtil {
    public static  final String ALI_DOMAIN = "https://heritage-hub.oss-cn-beijing.aliyuncs.com/";

    public  static  String uploadImage(MultipartFile file) throws IOException {
        String originFilename = file.getOriginalFilename();
        String ext = "."+ FilenameUtils.getExtension(originFilename);
        String uuid = UUID.randomUUID().toString().replace("-","");
        String fileName = uuid+ext;

        //地域节点
        String endpoint = "https://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5tMwL6VMWMCNboTLbTZs";
        String accessKeySecret="kFePJdZ7kLjkr40SrsHmVOC1sLnH2e";

        OSS ossClient= new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
        ossClient.putObject(
                "heritage-hub",
                fileName,
                file.getInputStream()
        );
        ossClient.shutdown();
        return  ALI_DOMAIN + fileName;
    }

}